package com.proactive.webapp.controller;

import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.proactive.webapp.dao.CourseCollectDAO;
import com.proactive.webapp.dao.DesignCollectDAO;
import com.proactive.webapp.dao.ResourceCollectDAO;
import com.proactive.webapp.dao.WorksheetCollectDAO;
import com.proactive.webapp.entity.CourseCollect;
import com.proactive.webapp.entity.DesignCollect;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.ResourceCollect;
import com.proactive.webapp.entity.WorksheetCollect;

@Controller
public class CollectController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	
	@RequestMapping(value = "/addCollect", method = RequestMethod.POST)
	public @ResponseBody void addCollect(int type, int id, HttpSession session){
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
			String loginAccount = loginMember.getAccount();
			if(type == 1){
				CourseCollectDAO courseCollectDAO = (CourseCollectDAO) context.getBean("courseCollectDAO");
				CourseCollect courseCollect = new CourseCollect();
				courseCollect.setAccount(loginAccount);
				courseCollect.setCid(id);
				courseCollectDAO.insert(courseCollect);
			}
			else if(type == 2){
				DesignCollectDAO designCollectDAO = (DesignCollectDAO) context.getBean("designCollectDAO");
				DesignCollect designCollect = new DesignCollect();
				designCollect.setAccount(loginAccount);
				designCollect.setDid(id);
				designCollectDAO.insert(designCollect);
			}
			else if(type == 3){
				WorksheetCollectDAO worksheetCollectDAO = (WorksheetCollectDAO) context.getBean("worksheetCollectDAO");
				WorksheetCollect worksheetCollect = new WorksheetCollect();
				worksheetCollect.setAccount(loginAccount);
				worksheetCollect.setWid(id);
				worksheetCollectDAO.insert(worksheetCollect);
			}
			else if(type == 4){
				ResourceCollectDAO resourceCollectDAO = (ResourceCollectDAO) context.getBean("resourceCollectDAO");
				ResourceCollect resourceCollect = new ResourceCollect();
				resourceCollect.setAccount(loginAccount);
				resourceCollect.setRid(id);
				resourceCollectDAO.insert(resourceCollect);
			}
		}
	}

	@RequestMapping(value = "/delCollect", method = RequestMethod.POST)
	public @ResponseBody void delCollect(int type, int id, HttpSession session){
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
			String loginAccount = loginMember.getAccount();
			if(type == 1){
				CourseCollectDAO courseCollectDAO = (CourseCollectDAO) context.getBean("courseCollectDAO");
				CourseCollect courseCollect = new CourseCollect();
				courseCollect.setAccount(loginAccount);
				courseCollect.setCid(id);
				courseCollectDAO.delete(courseCollect);
			}
			else if(type == 2){
				DesignCollectDAO designCollectDAO = (DesignCollectDAO) context.getBean("designCollectDAO");
				DesignCollect designCollect = new DesignCollect();
				designCollect.setAccount(loginAccount);
				designCollect.setDid(id);
				designCollectDAO.delete(designCollect);
			}
			else if(type == 3){
				WorksheetCollectDAO worksheetCollectDAO = (WorksheetCollectDAO) context.getBean("worksheetCollectDAO");
				WorksheetCollect worksheetCollect = new WorksheetCollect();
				worksheetCollect.setAccount(loginAccount);
				worksheetCollect.setWid(id);
				worksheetCollectDAO.delete(worksheetCollect);
			}
			else if(type == 4){
				ResourceCollectDAO resourceCollectDAO = (ResourceCollectDAO) context.getBean("resourceCollectDAO");
				ResourceCollect resourceCollect = new ResourceCollect();
				resourceCollect.setAccount(loginAccount);
				resourceCollect.setRid(id);
				resourceCollectDAO.delete(resourceCollect);
			}
		}
	}
}
